package com.aida.evil.eda.Network;


import android.app.Activity;

import com.aida.evil.eda.Data.NetworkData;
import com.aida.evil.eda.Toast.CustomSuperCardToast;
import com.aida.evil.eda.Volley.GsonRequest;
import com.aida.evil.eda.Volley.VolleyApplication;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.List;

/****************************************************
 * com.aida.evil.eda.answer.Answer_item_nerwork
 * 作者：白雨濃  evil
 * 时间：16-2-20 09:43
 * 邮箱：evilbai@foxmail.com
 * 页面用途：答题页面的网络加载
 * 包含ItemBean等内部类，用于支撑Json
 ****************************************************/
public abstract class Answer_item_nerwork
{
    private final static String URL = "http://" + NetworkData.ServiceIP + "/AnswerItem?itemID=";


    private Activity activity;

    private int itemID;
    private List<AnswerList> answerList_list;


    public Answer_item_nerwork(Activity activity, int itemID)
    {
        this.activity = activity;
        this.itemID = itemID;
        toNetwork();
    }

    public abstract void Responselistener(int itemID, int size, int[] AnswerIDs
            , String[] AnswerQuestions, String[] AnswerYess, String[] AnswerNos);

    public void toNetwork()
    {
        Response.Listener listener = new Response.Listener<Answer>()
        {
            @Override
            public void onResponse(Answer answer)
            {
                answerList_list = answer.getAnswerItem_list();
                int size = answer.getSize();
                int[] AnswerIDs = new int[size];
                String[] AnswerQuestions = new String[size];
                String[] AnswerYess = new String[size];
                String[] AnswerNos = new String[size];
                if (size != 0)
                {
                    /**
                     * 解析变量
                     */
                    AnswerList answerList;
                    for (int i = 0; i < size; i++)
                    {
                        answerList = answerList_list.get(i);
                        AnswerIDs[i] = answerList.getAnswerID();
                        AnswerQuestions[i] = answerList.getAnswerQuestion();
                        AnswerYess[i] = answerList.getAnswerYes();
                        AnswerNos[i] = answerList.getAnswerNo();
                    }
                    Responselistener(answer.getItemID(), size
                            , AnswerIDs, AnswerQuestions, AnswerYess, AnswerNos);
                }
                else
                {
                    CustomSuperCardToast.makeText(activity, "暂时没有内容", "好吧……").show();
                }
            }
        };

        Response.ErrorListener errorListener = new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError volleyError)
            {
                CustomSuperCardToast.makeText(activity, "加载失败", "好吧……").show();
            }
        };

        GsonRequest<Answer> answerGsonRequest = new GsonRequest<>(Request.Method.GET, URL + itemID, Answer.class, listener, errorListener);
        answerGsonRequest.setTag("answerGsonRequest_AnswerActity");
        //Log.i("URL + itemID", "" + URL + itemID);
        VolleyApplication.getQueue().add(answerGsonRequest);
    }


    /********************************************************************************************
     * ItemBean可继承的内部类
     */
    private class Answer
    {

        private int itemID;
        private int size;
        private List<AnswerList> answerItem_list = new ArrayList<>();

        public int getSize()
        {
            return size;
        }

        public void setSize(int size)
        {
            this.size = size;
        }


        public int getItemID()
        {
            return itemID;
        }

        public void setItemID(int itemID)
        {
            this.itemID = itemID;
        }

        public List<AnswerList> getAnswerItem_list()
        {
            return answerItem_list;
        }

        public void setAnswerItem_list(List<AnswerList> answerItem_list)
        {
            this.answerItem_list = answerItem_list;
        }


    }

    protected class AnswerList
    {
        private int AnswerID;
        private String AnswerQuestion;
        private String AnswerYes;
        private String AnswerNo;

        public AnswerList()
        {
        }

        public AnswerList(int answerID, String answerQuestion, String answerYes,
                          String answerNo)
        {
            AnswerID = answerID;
            AnswerQuestion = answerQuestion;
            AnswerYes = answerYes;
            AnswerNo = answerNo;
        }

        public int getAnswerID()
        {
            return AnswerID;
        }

        public void setAnswerID(int answerID)
        {
            AnswerID = answerID;
        }

        public String getAnswerQuestion()
        {
            return AnswerQuestion;
        }

        public void setAnswerQuestion(String answerQuestion)
        {
            AnswerQuestion = answerQuestion;
        }

        public String getAnswerYes()
        {
            return AnswerYes;
        }

        public void setAnswerYes(String answerYes)
        {
            AnswerYes = answerYes;
        }

        public String getAnswerNo()
        {
            return AnswerNo;
        }

        public void setAnswerNo(String answerNo)
        {
            AnswerNo = answerNo;
        }

    }

}

